package com.example.zrq1010110087.graceplayer.data;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import com.example.zrq1010110087.graceplayer.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by zrq1010110087 on 2017/10/16.
 *  This is a MediaPlayerUser which invoke the method of MediaPlayer to control music
 *
 */

public class MediaPlayerUser {
   //音乐播放状态
    public static final int MUSIC_STATUS_UNLOAD=0;
    public static final  int MUSIC_STATUS_PAUSED =1;
    public static final  int MUSIC_STATUS_STOP=3;
    public static final  int MUSIC_STATUS_COMPLETED=4;
    public static final  int MUSIC_STATUS_STARTED =5;
    public static final  int MUSIC_STATUS_INITIALIZED  =6;
    public static final  int MUSIC_STATUS_PREPARED =7;
    public static final  int MUSIC_STATUS_PREPARING =8;
    public static final  int MUSIC_STATUS_IDLE =9;
    public static final  int MUSIC_STATUS_END =10;
    public static final int MUSIC_STATUS_ERROR=-2;

    //播放命令
    public static final  int MUSIC_COMMAND_PALY=0;
    public static final  int MUSIC_COMMAND_PAPUSE=1;
    public static final  int MUSIC_COMMAND_RESUME=2;
    public static final  int MUSIC_COMMAND_STOP=3;
    public static final  int MUSIC_COMMAND_PREVIOUS=4;
    public static final  int MUSIC_COMMAND_NEXT=5;
    public static final int MUSIC_COMMAND_ONCLICKITEM_NUMBER=6;
    public static final int MUSIC_COMMAND_SEEKTO=7;
    public static final int MUSIC_COMMAND_PLAYMODEL=8;
    public  static final int MUSIC_COMMAND_COMPLETED=9;
    //播放模式
    public static final int MUSIC_PLAYMODEL_SEQUENCE=0;
    public static final int MUSIC_PLAYMODEL_RANDOM=1;
    public static final int MUSIC_PLAYMODEL_SINGLE=2;
    public static final int MUSIC_PLAYMODEL_LIST=3;
    //全局变量
    private  MediaPlayer mediaPlayer=new MediaPlayer();
    private ArrayList<Music> arrayList;
    private Context context;
    private  volatile int number=0;
    private  int status=MUSIC_STATUS_UNLOAD;//音乐播放器的状态
    private  int playModel=MUSIC_PLAYMODEL_SEQUENCE;//默认顺序播放


    //初始化音乐列表类
    public MediaPlayerUser(ArrayList<Music> arrayList,Context context){

        this.arrayList=arrayList;
        this.context=context;
        setOnCompletionListener();
        setOnSeekCompleteListener();


    }




    //设置number
    public void setMusicNumber(int number){

        this.number=number;

    }
//获取当前音乐歌曲名
    public String getCurrentMusicName() {


        return  arrayList.get(number).getmusicName();
    }
//获得当前音乐艺术家名
    public String getCurrentMusicArtist() throws IOException {


        return  arrayList.get(number).getmusicArtist();
    }
//获得当前音乐总时长
    public int getCurrentMusicDuration() throws IOException {


        return   arrayList.get(number).getmusicDuration();
    }


    //得到播放歌曲序号
    public int getMusicNumber(){

        return number;

    }

     //加载音乐
    public void load()  {
      try {

             //进入游离态
              mediaPlayer.reset();
              //进入初始态
              mediaPlayer.setDataSource(arrayList.get(number).getmusicPath());
              //进入准备态
              mediaPlayer.prepare();

            status=MUSIC_STATUS_PREPARED;




      }catch (Exception e){

          e.printStackTrace();
      }
    }

    //播放音乐
    public void play() {

        if (mediaPlayer != null && mediaPlayer.isPlaying()) {

           stop();
        }
            load();

            mediaPlayer.start();
        if(status!= MUSIC_STATUS_STARTED)
        status= MUSIC_STATUS_STARTED;
            }

      //暂停音乐
        public void pause () {

                mediaPlayer.pause();
            status= MUSIC_STATUS_PAUSED;
        }
    //恢复音乐播放
    public void resume () {

        mediaPlayer.start();
        status= MUSIC_STATUS_STARTED;
    }
    //播放,暂停,恢复 音乐
    public  void play_pause_resume(){

             if(mediaPlayer!=null){

                    if(mediaPlayer.isPlaying()){

                        pause();
                    }else if(status== MUSIC_STATUS_PAUSED){

                        resume();

                    }else if(status==MUSIC_STATUS_STOP){

                        play();

                    }else{

                        play();
                     }

             }


    }
        //停止音乐
        public void stop () {

            if(status== MUSIC_STATUS_STARTED ||status== MUSIC_STATUS_PAUSED) {
                mediaPlayer.stop();
                status = MUSIC_STATUS_STOP;
            }
        }
        //获取当前播放时间点
        public  int getCurrentTime(){

            return  mediaPlayer.getCurrentPosition();
        }


        //播放上一首
       public void moveToPrevious(){

           //若到第一首,提醒用户
           if(number==0){

               Toast.makeText(context,context.getString(R.string.musiclist_top),Toast.LENGTH_SHORT).show();
           }else{
               --number;

               play();

           }


       }
       //播放下一首
        public void moveToNext(){

            //若到最后一首,提醒用户
            if(number==arrayList.size()-1){

                Toast.makeText(context,context.getString(R.string.musiclist_bottom),Toast.LENGTH_SHORT).show();
            }else {

                ++number;
                play();
            }


            }
    //顺序播放
    public void playModel_sequence(){

        moveToNext();

    }
    //随机播放
    public void playModel_random(){

        number=new Random().nextInt(10000)%arrayList.size();
        play();

    }
    //单曲播放
    public void playModel_single(){

        mediaPlayer.start();
        status= MUSIC_STATUS_STARTED;

    }
    //列表播放
    public void playModel_List(){

        if(number==arrayList.size()-1){
            number=0;
            play();
        }else {
            ++number;
            play();
        }

        }



        public void onClickListItem(int number){

            setMusicNumber(number);
            play();


        }
        //跳转到音乐指定位置
        public void seekTo(int mesc){


                 mediaPlayer.seekTo(mesc);


             }


    //获取当前播放状态
        public  int getMusicStatus(){

            return  status;
        }
        //判断播放状态
        public  boolean getPlaying(){

            return mediaPlayer.isPlaying();

        }

        public  void releaseMediaPlayer(){

            if(mediaPlayer!=null)
            mediaPlayer.release();
            mediaPlayer=null;


        }
        //设置播放模式
        public void setPlayModel(int playModel){

        this.playModel=playModel;
    }
    //歌曲播放结束后,进行播放模式的判断,再进行播放歌曲
        public void setOnCompletionListener(){

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {

                    status=MUSIC_STATUS_COMPLETED;
                    switch (playModel){

                        case MUSIC_PLAYMODEL_SEQUENCE: playModel_sequence();break;
                        case MUSIC_PLAYMODEL_RANDOM: playModel_random();break;
                        case MUSIC_PLAYMODEL_SINGLE: playModel_single();break;
                        case MUSIC_PLAYMODEL_LIST: playModel_List();break;
                    }
                }
            });
    }

    public void setOnSeekCompleteListener() {
        mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mediaPlayer) {


            }
        });
    }


}
